class CustomEventPromise {
    constructor() {
        this.events = {};
        this.promise = null;
    }

    // 创建Promise
    createPromise() {
        this.promise = new Promise((resolve, reject) => {
            this.resolve = resolve;
            this.reject = reject;
        });
        return this.promise;
    }

    // 添加事件监听
    on(event, callback) {
        if (!this.events[event]) {
            this.events[event] = [];
        }
        this.events[event].push(callback);
    }

    // 移除事件监听
    off(event, callback) {
        if (this.events[event]) {
            this.events[event] = this.events[event].filter(cb => cb !== callback);
        }
    }

    // 触发事件
    emit(event, data) {
        if (this.events[event]) {
            this.events[event].forEach(callback => callback(data));
        }
    }

    // 触发成功
    resolve(data) {
        this.emit('success', data);
        this.resolve(data);
    }

    // 触发失败
    reject(error) {
        this.emit('error', error);
        this.reject(error);
    }
}

// 使用示例
const customPromise = new CustomEventPromise();
const promise = customPromise.createPromise();

customPromise.on('success', (data) => {
    console.log('Success event:', data);
});

customPromise.on('error', (error) => {
    console.log('Error event:', error);
});

promise.then(data => {
    console.log('Promise resolved:', data);
}).catch(error => {
    console.log('Promise rejected:', error);
});

customPromise.resolve('Hello World');